package com.ks.servlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.log.Log;

import com.ks.entity.User;
import com.ks.service.UserService;
import com.ks.utils.CookieUtils;
import com.ks.utils.Factory;

import common.Var;

/**
 * 前端传递的一个method参数与方法名对应
 * 
 * @author 47475
 *
 */
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	UserService userServiceImpl = (UserService) Factory.getBean("userServiceImpl");

	protected void checkUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	protected void deleteUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("delete");
		String username = request.getParameter("username");
		int ret = userServiceImpl.deleteUser(username);
		System.out.println(ret);
		if (ret == 1) {
			List<User> userList = userServiceImpl.getAllUser();
			request.setAttribute(Var.USERLIST_INFO, userList);
			request.getRequestDispatcher("/jsp/alluser.jsp").forward(request, response);

		}
	}

	protected void updateUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("update");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int age = Integer.parseInt(request.getParameter("age"));
		String[] hobbys= request.getParameterValues("hobby");
		String hobby = Arrays.toString(hobbys);
		String address = request.getParameter("address");
		String img = request.getParameter("img");
		User user = new User(username, password, age, hobby, address, img);
		int ret = userServiceImpl.updateUser(user);
		System.out.println(ret);
		if (ret == 1) {
			/** 如果用户信息更新成功，更新session的信息 */
			request.getSession(false).setAttribute(Var.USER_INFO, user);
			/** 更新cookie中存的密码 */
			CookieUtils.addCookie(request, response, Var.PASSWORD, password, 3600,true);
			request.getCookies();
			request.setAttribute(Var.USER_INFO, user);
			request.getRequestDispatcher("/jsp/personal.jsp").forward(request, response);
		} else {
		}
	}

	protected void showUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("showUser");
		/** 从session中获取用户名 */
		String username = ((User) request.getSession().getAttribute(Var.USER_INFO)).getUsername();
		User user = userServiceImpl.getUserById(username);
		if (user != null) {
			request.setAttribute(Var.USER_INFO, user);
			request.getRequestDispatcher("/jsp/personal.jsp").forward(request, response);
		} else {
			Log.debug("查询的用户信息为null");
		}
	}

	protected void showAllUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("showAllUser");
		List<User> userList = userServiceImpl.getAllUser();
		if (userList != null) {
			request.setAttribute(Var.USERLIST_INFO, userList);
			request.getRequestDispatcher("/jsp/alluser.jsp").forward(request, response);
		} else {
			Log.debug("查询的用户列表为null");
		}
	}

}
